package com.example.config.advice;

import com.example.exception.ServiceException;
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.server.exceptions.ExceptionHandler;
import jakarta.inject.Singleton;
import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常处理
 *
 * @Author: scy
 * @Date: 2022-07-29 10:22
 */
@Slf4j
@Produces
@Singleton
public class GlobalExceptionHandler implements ExceptionHandler<ServiceException, HttpResponse<String>> {

    @Override
    public HttpResponse<String> handle(HttpRequest request, ServiceException exception) {
        log.error("请求异常：", exception);
        return HttpResponse.status(HttpStatus.BAD_REQUEST)
                .body("异常信息：" + exception.getMessage());
    }
}
